package com.recyclebanner;

import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RelativeLayout;

import com.hxkcloud.patient.R;

import java.util.List;

/**
 * 初始化无效循环的banner
 * Created by cys on 2015/11/4.
 */
public class InitRecycleViewpager implements ViewPager.OnPageChangeListener {
    public InitRecycleViewpager() {

    }

    //private static InitRecycleViewpager instance;

    public InfiniteViewPager getInfinite_Pager() {
        return infinite_Pager;
    }

    private InfiniteViewPager infinite_Pager;

    public ImageCountView getImage_dian() {
        return image_dian;
    }

    private ImageCountView image_dian;
    private RelativeLayout banner_height;
    private Activity context;
    private List list;
    private RecycleBannerAdapter adapter;
    private int scrollTime = 5000;
    private boolean isAutoscrooll = false;

    /**
     * 设置自动轮播
     */
    public void startAutoscrooll() {
        isAutoscrooll = true;
   /*     if (isAutoscrooll){
            return;
        }*/

        if (list != null && list.size() != 0) {
            //  infinite_Pager.stopAutoScroll();
            infinite_Pager.setAutoScrollTime(scrollTime);
            infinite_Pager.startAutoScroll();
        }

    }

    /**
     * 停止滚动
     */
    public void stopAutoScroll() {
        infinite_Pager.stopAutoScroll();
    }

    public void setNull() {
        adapter=null;
        infinite_Pager =null;
        image_dian = null;
        banner_height = null;
        adapter=null;
        list=null;
    }

    /**
     * 设置自动轮播时间
     */
    public void setAutoscroollTime(int scrollTime) {
        this.scrollTime = scrollTime;
    }


    /**
     * 初始化布局
     */
    public void initView(Activity context) {
        this.context = context;
        infinite_Pager = (InfiniteViewPager) context.findViewById(R.id.infinite_Pager);
        image_dian = (ImageCountView) context.findViewById(R.id.image_dian);
        banner_height = (RelativeLayout) context.findViewById(R.id.banner_height);

        list = null;
    }

    /**
     * 初始化布局
     */
    public void initView(Activity context, View contextView) {
        this.context = context;
        infinite_Pager = (InfiniteViewPager) contextView.findViewById(R.id.infinite_Pager);
        image_dian = (ImageCountView) contextView.findViewById(R.id.image_dian);
        banner_height = (RelativeLayout) contextView.findViewById(R.id.banner_height);
        list = null;
    }

    /**
     * 设置banner的高度需要在intView方法之后
     *
     * @param height 高度px
     */
    public void setBannerHeight(int height) {
        if (banner_height != null) {
            RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, height);
            banner_height.setLayoutParams(layout);
        }

    }

    /**
     * 填充数据源
     *
     * @param resId         主布局banner 的item
     * @param list          数据集合
     * @param item_response 接口回调，回调item的布局和位置
     */
    public void initData(int resId, List list, BannerItemResponse item_response) {
        this.list = list;
        if (list != null && list.size() != 0) {
            InfiniteViewPager.FakePositionHelper.MULTIPLIER = 5;
            if(list.size() == 1)
            {
                InfiniteViewPager.FakePositionHelper.MULTIPLIER = 1;
            }

            adapter = new RecycleBannerAdapter(resId, list, context);
            if (item_response != null) {
                adapter.setItemClickPostion(item_response);
            }

            infinite_Pager.setOnPageChangeListener(this);
            infinite_Pager.setAdapter(adapter);
            image_dian.setCountNum(list.size());
            image_dian.setSelectOrder(0);

        }

    }

    /**
     * 设置当前的位置
     */
    public void setCurrentPosition(int position) {
        if (infinite_Pager != null)
            infinite_Pager.setCurrentItem(position);
    }

    /**
     * 数据刷新
     */
    public void notifyDataSetChanged() {
        if (list != null && list.size() != 0) {
            adapter.notifyDataSetChanged();
            image_dian.setCountNum(list.size());
            image_dian.setSelectOrder(0);
        }
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (image_dian!=null)
        image_dian.setSelectOrder(position);
        if (infinite_Pager != null)
            infinite_Pager.setCurrentItem(position);
        if (pageChangeListener!=null)
            pageChangeListener.onPageSelected(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    /**
     * 回调循环banner每个item的布局和位置
     */
    public interface BannerItemResponse {
        public void responseData(View convertView, int position);
    }
    private onCustomPageChangeListener pageChangeListener;

    public void setPageChangeListener(onCustomPageChangeListener pageChangeListener) {
        this.pageChangeListener = pageChangeListener;
    }

    public interface onCustomPageChangeListener{
        void  onPageSelected(int position);
    }
}
